Ext.define('OrderConvert.view.convert.ConvertGridController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.convert-convertgrid',

    reload : function () {
        var grids = Ext.ComponentQuery.query("convertgrid");
        if(grids.length > 0){
            grids[0].getStore().reload();
        }
    },

    addConvert : function () {
        var widget = Ext.widget("convertaddformwin");
        widget.show();
    },

    editRule : function (btn) {
        var grid = btn.up("grid");
        var records = grid.getSelection();
        if(records.length > 0){
            var record = records[0];
            var sheetId = record.get("sheetId");
            if(sheetId){
                var widget = Ext.widget("convertaddformwin",{
                    title : '编辑规则',
                    buttons : [{
                        text : '提交',
                        handler : 'update'
                    },{
                        text : '关闭',
                        handler : 'close'
                    }]
                });
                var form = widget.down("form");
                form.down("filefield[name=xls]").disable();
                form.down("filefield[name=file]").disable();
                form.down("hidden[name=rule.id]").setValue(record.get("id"));
                form.down("textfield[name=rule.name]").setValue(record.get("name"));
                form.down("textfield[name=rule.apiUrl]").setValue(record.get("apiUrl"));
                widget.show();
            }else{
                Ext.Msg.alert("提示","该规则无效！");
            }
        }else{
            Ext.Msg.alert("提示","请选择规则！");
        }
    },

    editRuleArea : function (btn) {
        var grid = btn.up("grid");
        var records = grid.getSelection();
        if(records.length > 0){
            var record = records[0];
            var sheetId = record.get("sheetId");
            if(sheetId){
                window.open(OrderConvertUrl.constant.sheetServer + "sheet?editFileId=" + sheetId);
            }else{
                Ext.Msg.alert("提示","该规则无效！");
            }
        }else{
            Ext.Msg.alert("提示","请选择规则！");
        }
    },
    

    deleteRule : function (btn) {
        var grid = btn.up("grid");
        var records = grid.getSelection();
        if(records.length > 0){
            Ext.Msg.confirm('提示', '是否确定该操作？',
                function (choice){
                    if(choice == "yes"){
                        var record = records[0];
                        var id = record.get("id");
                        Ext.Ajax.request({
                            url: OrderConvertUrl.constant.serverUrl + 'convert/delete?id=' + id,

                            success: function(response, opts) {
                                var obj = Ext.decode(response.responseText);
                                grid.getStore().load();
                            },

                            failure: function(response, opts) {
                                console.log('server-side failure with status code ' + response.status);
                            }
                        });
                    }
                })

        }else{
            Ext.Msg.alert("提示","请选择规则！");
        }
    }


});
